package com.leyou.item.web;

import com.leyou.common.dto.CartDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "商品接口")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询SPU
     * @param page
     * @param rows
     * @param saleable
     * @param desc
     * @param key
     * @return
     */
    @ApiOperation("分页查询SPU")
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<Spu>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            //@RequestParam(value = "desc",defaultValue = "1") Boolean desc,
            @RequestParam(value = "key",required = false) String key){

        return ResponseEntity.ok(goodsService.querySpuByPage(page,rows,saleable,key));
    }

    /**
     * 商品的新增
     * @param spu
     * @return
     */
    @ApiOperation("商品的新增")
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody Spu spu){
        goodsService.saveGoods(spu);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 商品的删除 设置valid为false
     * @param id
     * @return
     */
    @ApiOperation("商品的删除 设置valid为false  数据库为0")
    @DeleteMapping("goods/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id") Long id){
        goodsService.deleteGoods(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 商品的修改
     * @param spu
     * @return
     */
    @ApiOperation("商品的修改")
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody Spu spu){
        goodsService.updateGoods(spu);
        return ResponseEntity.status(HttpStatus.OK).build();
    }


    //点击修改时 需要查询spu和sku
    /**
     * 根据spu的id查询详情detail
     * @param spuId
     * @return
     */
    @ApiOperation("根据spu的id查询详情detail")
    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> queryDetailById(@PathVariable("id") Long spuId){
        return ResponseEntity.ok(goodsService.queryDetailById(spuId));
    }

    /**
     * 根据spuId查询下面的所有sku
     * @param spuId
     * @return
     */
    @ApiOperation("根据spuId查询下面的所有sku")
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySkuBySpuId(spuId));
    }

    /**
     * 根据sku的id集合查询所有sku
     * @param ids
     * @return
     */
    @ApiOperation("根据sku的id集合查询所有sku")
    @GetMapping("/sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     * 商品下架
     * @param id
     * @return
     */
    @ApiOperation("商品下架")
    @PostMapping("unsaleable/{id}")
    public ResponseEntity<Void> unsaleable(@PathVariable("id") Long id){
        goodsService.unSaleAble(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /**
     * 商品上架
     * @param id
     * @return
     */
    @ApiOperation("商品上架")
    @PostMapping("saleable/{id}")
    public ResponseEntity<Void> saleable(@PathVariable("id") Long id){
        goodsService.saleAble(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 根据id查询spu  搜索
     * @param id
     * @return
     */
    @ApiOperation("根据id查询spu  搜索")
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 减库存 下单
     * @param carts
     * @return
     */
    @ApiOperation("减库存")
    @PostMapping("stock/decrease")
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDTO> carts){
        goodsService.decreaseStock(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}












